
package com.scalar.chiptrack;

/**
 * Author: Balaram Putta
 * Date: Oct 21, 2003
 * Time: 10:40:12 AM
 */

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import com.scalar.chiptrack.commons.ErrorMessages;
import com.scalar.chiptrack.commons.InfoMessages;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.users.User;


/**
 * This class extends the Action class in STRUTS Framework.
 * All action classes in ChipTrac must extend this class.
 *
 * @author 	Balaram Putta
 * @version
 */

public abstract class ChipTrackExtendedAction extends Action
                                              implements ErrorMessages, InfoMessages, SessionKeys, Constants
{
    protected HttpSession pSession = null;
    protected User pUser = null;

    /**
     * This method has to be implemented all action classes to provide business logic
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public abstract ActionForward executeRequest( ActionMapping mapping,
                                                  ActionForm form,
                                                  HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception;


    /**
     * This method is called by the controller servlet in order to transfer the control to the Action class
     * Its called for all requests sent to each and every action class
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response )
        throws Exception
    {
        // Assign current session
        pSession = request.getSession();

        // Get current user
        pUser = ( User ) pSession.getAttribute( USER_KEY );

        // Redirect the user to login page, if he/she is not logged in
        if( pUser == null )
        {
            pSession.setAttribute( SOURCE_PAGE_KEY, request.getServletPath() + "?" + request.getQueryString() );
            return( mapping.findForward( ACTION_FORWARD_KEY_LOGIN ) );
        }

        // Create and add Errors HashMap to the request
        request.setAttribute( ERROR_MESSAGES, new HashMap() );

        // Create and add Info Messages HashMap to the request
        request.setAttribute( INFO_MESSAGES, new HashMap() );


        // Send the request for actual processing
        return executeRequest( mapping, form, request, response );

    }
}